import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children: [
      {
        path: '/',
        name: 'Main',
        component: () => import('../views/Main.vue'),
        children:[
          {
            path:"/hou",
            name:"Hou",
            component:() => import("../views/Hou.vue")
          },
          {
            path:"/photo",
            name:"Photo",
            component:() => import("../views/Photo.vue")
          },
          {
            path:"/list",
            name:"List",
            component:() => import("../views/List.vue")
          }
        ]
      },{
        path: '/store',
        name: 'Store',
        component: () => import('../views/Store.vue')
      },{
        path: '/order',
        name: 'Order',
        component: () => import('../views/Order.vue')
      },{
        path: '/member',
        name: 'Member',
        component: () => import('../views/Member.vue')
      },{
        path: '/setting',
        name: 'Setting',
        component: () => import('../views/Setting.vue')
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  var userLoginStatus = localStorage.getItem("token");
  if (to.name === "Login") {
    next();
  } else {
    if (userLoginStatus) {
      next();
    } else {
      next("/login")
    }
  }
})

export default router
